import time
import threading

import numpy
from memory_profiler import profile


class MemoryMonitorManager(object):
    def __init__(self):
        self.memory_list = []
        # threading.Thread(target=self.print_memory_used).start()

    def add_memory(self):
        np_a = numpy.array((30, 30))
        n = 0
        while True:
            self.memory_list.append(np_a)
            # time.sleep(0.00001)
            n += 1
            if n >= 10000000:
                break

    @profile
    def get_memory(self):
        return self.memory_list

    def print_memory_used(self):
        # snapshot = tracemalloc.take_snapshot()
        while True:
            # try:
            #     snapshot1 = tracemalloc.take_snapshot()
            #     top_stats = snapshot1.compare_to(snapshot, 'lineno')
            #     # logger.warning("[ Top 20 differences ]")
            #     for stat in top_stats[:20]:
            #         if stat.size_diff < 0:
            #             continue
            #         # logger.warning(stat)
            #         print(stat)
            #     print("---------------------------")
            #     # snapshot = tracemalloc.take_snapshot()
            #
            # except Exception as e:
            #     print(e)
            #     pass

            time.sleep(5)


if __name__ == '__main__':
    memory_test = MemoryMonitorManager()
    memory_test.add_memory()
    memory_test.get_memory()
